package com.rykj.qxj.server.util.date;


import com.rykj.qxj.server.util.object.ObjectUtils;
import com.rykj.qxj.server.util.object.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * @author jixiang
 * <p>
 * <p>
 * 时间格式转化工具
 */
public class DateUtils {
    public final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_FORMAT_SIMAPLE_HM = "yyyy-MM-dd HH:mm";
    public final static String DATE_FORMAT_SIMAPLE = "yyyy-MM-dd";
    public final static String DATE_FORMAT_SIMAPLE_CH = "yyyy年MM月dd日";
    public final static String DATE_FORMAT_DAY = "yyyy-MM-dd";
    public final static String DATE_FORMAT_MONTH = "yyyy-MM";
    public final static String DATE_DEFAULT = "1970-01-01 12:00:00";
    public final static String DATE_FORMAT_HOURS = "HH";

    /**
     * @param ts 要传入字符串类型的时间戳格式为XXXX-XX-XX HH:MM:SS
     */
    public static long getTimeStamp(String ts) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.CHINA);
        Date start = sdf.parse(ts);
        return start.getTime();
    }

    /**
     * 将long型的时间戳转换为YYYY-MM-DD HH:MM:SS格式的日期字符串
     *
     * @param timestamp
     * @return date_string
     */
    public static String getDate(long timestamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        Date dt = new Date(timestamp);
        return dateFormat.format(dt);
    }

    /**
     * 将long型的时间戳转换为YYYY-MM-DD格式的日期字符串
     *
     * @param timestamp
     * @return date_string
     */
    public static String getDateDay(long timestamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_DAY);
        Date dt = new Date(timestamp);
        return dateFormat.format(dt);
    }

    /**
     * 将long型的时间戳转换为YYYY-MM格式的日期字符串
     *
     * @param timestamp
     * @return date_string
     */
    public static String getDateMonth(long timestamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_MONTH);
        Date dt = new Date(timestamp);
        return dateFormat.format(dt);
    }

    /**
     * 将long型的时间戳转换为HH:MM格式的日期字符串
     *
     * @param timestamp
     * @return date_string
     */
    public static String getDateHours(long timestamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_HOURS);
        Date dt = new Date(timestamp);
        return dateFormat.format(dt);
    }


    /**
     * 方法描述:通过date获取时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月18日 下午3:08:05<br/>
     * Version:1.0 <br/>
     *
     * @param date
     * @return 数据格式为'yyyy-MM-dd hh:mm:ss' 时间
     */
    public static String getDate(Date date) {
        return getDate(date, DATE_FORMAT);
    }

    /**
     * 方法描述:通过date获取时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014-9-12 下午2:18:21<br/>
     * Version:1.0 <br/>
     *
     * @param date    需格式化的时间
     * @param pattern 时间格式
     * @return 返回为pattern的时间
     */
    public static String getDate(Date date, String pattern) {
        if (ObjectUtils.isEmpty(date)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * 方法描述:通过date获取时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月18日 下午3:08:05<br/>
     * Version:1.0 <br/>
     *
     * @param date 需格式化时间,默认格式为 'yyyy-MM-dd hh:mm:ss'
     * @return
     * @throws ParseException
     */
    public static Date getDate(String date) throws ParseException {
        return getDate(date, DATE_FORMAT);
    }

    /**
     * 方法描述:通过date获取时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月18日 下午3:08:05<br/>
     * Version:1.0 <br/>
     *
     * @param date    需格式化时间,默认格式为 'yyyy-MM-dd hh:mm:ss'
     * @param pattern 时间格式
     * @return
     * @throws ParseException
     */
    public static Date getDate(String date, String pattern) throws ParseException {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(StringUtils.isEmpty(pattern) ? DATE_FORMAT : pattern);
        return dateFormat.parse(date);
    }

    /**
     * 方法描述:获取当前时间<br/>
     * Author:<a href="lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月11日 上午11:10:40<br/>
     * Version:1.0 <br/>
     *
     * @return 当前时间格式:pattern
     */
    public static String getNowDate(String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(new Date());
    }

    /**
     * 方法描述:获取当前时间<br/>
     * Author:<a href="lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月11日 上午11:10:40<br/>
     * Version:1.0 <br/>
     *
     * @return 当前时间格式"yyyy-MM-dd hh:mm:ss"
     */
    public static String getNowDate() {
        return getNowDate(DATE_FORMAT);
    }

    /**
     * 添加多少年
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowYear(Date now, int amount) {
        return addHow(now, Calendar.YEAR, amount);
    }

    /**
     * 添加多少月
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowMonth(Date now, int amount) {
        return addHow(now, Calendar.MONTH, amount);
    }

    /**
     * 添加多少周
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowWeek(Date now, int amount) {
        return addHow(now, Calendar.WEEK_OF_MONTH, amount);
    }

    /**
     * 添加多少天
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowDay(Date now, int amount) {
        return addHow(now, Calendar.DAY_OF_MONTH, amount);
    }

    /**
     * 添加多少小时
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowHour(Date now, int amount) {
        return addHow(now, Calendar.HOUR_OF_DAY, amount);
    }

    /**
     * 添加多少分钟
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowMinute(Date now, int amount) {
        return addHow(now, Calendar.MINUTE, amount);
    }

    public static Date addHowSecond(Date now, int amount) {
        return addHow(now, Calendar.SECOND, amount);
    }

    /**
     * 在当前时间添加多少数量
     *
     * @param field
     * @param amount
     * @return
     */
    public static Date addHow(int field, int amount) {
        return addHow(new Date(), field, amount);
    }

    public static Date addHow(Date now, int field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    public static Date parseDate(String date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static int getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 获取周时间段
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInWeek(Date date) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置周
        calendar.setTime(date);
        //设置为星期一(一周第一天是星期天所以星期一就是2)
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        res[0] = calendar.getTime();
        //设置为星期天(在当前时间往后加6天就是星期天)
        calendar.add(Calendar.DAY_OF_YEAR, 6);
        res[1] = calendar.getTime();
        return res;
    }

    /**
     * 获取周范围时间段的起止时间
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInWeek(Date date, int week) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置周
        calendar.setTime(date);
        //设置为星期一(一周第一天是星期天所以星期一就是2)
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //设置为星期天(在当前时间往后加6天就是星期天)
        calendar.add(Calendar.DAY_OF_YEAR, 6);
        res[1] = calendar.getTime();
        calendar.add(Calendar.DAY_OF_YEAR, 7 * week);
        res[0] = calendar.getTime();
        return res;
    }

    /**
     * 获取月时间段
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInMonth(Date date) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置
        calendar.setTime(date);
        //设置为一号
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        res[0] = calendar.getTime();
        //设置为最后一天
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        res[1] = calendar.getTime();
        return res;
    }

    /**
     * 获取月时间段
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInMonth(Date date, int month) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置
        calendar.setTime(date);
        //设置为一号
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        res[0] = calendar.getTime();
        calendar.add(Calendar.MONTH, month);
        //设置为最后一天
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        res[1] = calendar.getTime();
        return res;
    }

    /**
     * 获取年时间段
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInYear(Date date) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置
        calendar.setTime(date);
        //设置为第一天
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        res[0] = calendar.getTime();
        //设置为最后一天
        calendar.add(Calendar.YEAR, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        res[1] = calendar.getTime();
        return res;
    }

    public static boolean equals(Date first_Date, Date second_Date) {
        String first_str = getDate(first_Date);
        String second_str = getDate(second_Date);
        return first_str.equals(second_str);
    }

    public static void main(String[] args) throws ParseException {
        Date date = DateUtils.getDate("2017-07-11 01:08:56", "yyyy-MM-dd HH:mm:ss");
        Date date1 = addHowMonth(date, 18);
        String date2 = getDate(date1, "yyyy-MM-dd HH:mm:ss");
        System.out.println(date2);
        //			System.out.println(DateUtils.getDate(date));
        //		System.out.println(	getTimeStamp("2015-01-06 11:11:00"));
        //        Date[] date = getFragmentInWeek(new Date(),-3);
        //        System.out.println("startTime"+date[0]);
        //        System.out.println("endTime:"+date[1]);
//        System.out.println(DateUtils.getDate(DateUtils.addHowDay(new Date(), -3), "yyyy-MM-dd"));

    }


    // 获得指定日期所在周- 周一  的日期
    public static String getWeekOfMonday(Date date) {
        int mondayPlus = getMondayPlus(date);
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        String preMonday = getDate(monday, DateUtils.DATE_FORMAT_DAY);
        return preMonday;
    }

    // 获得指定日期所在周- 周日  的日期
    public static String getWeekOfSunday(Date date) {
        int mondayPlus = getMondayPlus(date);
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
        Date monday = currentDate.getTime();
        String preMonday = getDate(monday, DateUtils.DATE_FORMAT_DAY);
        return preMonday;
    }

    // 获得指定日期与当时周一相差的天数
    public static int getMondayPlus(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    /**
     * 方法描述:获取当前时间前后指定时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2015-8-12 下午2:13:42<br/>
     * Version:1.0 <br/>
     *
     * @param day
     * @return
     */
    public static Date getDayDate(int day) {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DAY_OF_MONTH, day);
        return cd.getTime();
    }

    /**
     * 获取当天0点时间戳
     *
     * @return
     */
    public static Timestamp getDayBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 000);
        return new Timestamp(cal.getTimeInMillis());
    }
}
